package com.eques.demo.adapter.viewhoder

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.eques.demo.R
import com.eques.icvss.utils.ELog
import com.eques.impl.OnClickListenerImpl
import com.eques.util.EquesMytool

@Suppress("DEPRECATION")
class AnimalHoder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {

    private val tv_food_time: TextView = itemView!!.findViewById(R.id.tv_food_time)
    private val tv_food_week: TextView = itemView!!.findViewById(R.id.tv_food_week)
    private val img_switch: ImageView = itemView!!.findViewById(R.id.img_switch)

    @SuppressLint("SetTextI18n")
    fun setData(
        context: Context,
        dataBeans: List<String>,
        position: Int,
        onItemClickListener: OnClickListenerImpl
    ) {
        val data: String = dataBeans[position]
//         5c00010101
        tv_food_week.text = getWeek(
            context,
            data
        ) + " :" + context.getString(R.string.animal_food_num) + getIntNumber(data.substring(6, 8))
        tv_food_time.text = getNumber(data.substring(2, 4)) + ":" + getNumber(data.substring(4, 6))
        if (getStats(data)) {
            img_switch.setImageResource(R.mipmap.button_switch_on)
        } else {
            img_switch.setImageResource(R.mipmap.button_switch_off)
        }
        itemView.setOnClickListener(View.OnClickListener {
            onItemClickListener.CallBackItemClickListener(position, 0, 1)
        })
        itemView.setOnLongClickListener(View.OnLongClickListener {
            onItemClickListener.LongCallBackItemClickListener(position)
            true
        })
    }


    fun getNumber(data: String): String {
        var data = Integer.parseInt(data, 16)
        if (data < 10) {
            return "0$data"
        } else {
            return data.toString()
        }
    }

    fun getIntNumber(data: String): String {
        var data = Integer.parseInt(data, 16)
        if (data < 10) {
            return "$data"
        } else {
            return data.toString()
        }
    }

    /**
     * 获取状态
     */
    fun getStats(data: String): Boolean {
        var hexHourStr = data.substring(8, 10)
        return hexHourStr != "00"
    }

    /**
     * 获取周几
     */
    private fun getWeek(context: Context, data: String): String {
        var hexWeekStr = data.substring(0, 2)
        var binarySystem = EquesMytool(context).parseHexStr2Byte(hexWeekStr)
        var weekData = StringBuffer()
        var pos = 0
        for (i in binarySystem!!.subSequence(1, binarySystem.length)) {
            if (1 == Integer.parseInt(i.toString())) {
                weekData.append(context.getString(weeks[pos]))
                weekData.append(" ")
            }
            pos++
        }
        ELog.d("Week..result...", weekData.toString())
        return weekData.toString()
    }

    private val weeks = arrayOf(
        R.string.voice_service_monday,
        R.string.voice_service_tuesday,
        R.string.voice_service_wednesday,
        R.string.voice_service_thursday,
        R.string.voice_service_friday,
        R.string.voice_service_saturday,
        R.string.voice_service_sunday
    )
}